<?php
/**
 * EXCEL导入导出
 *
 * User: kaikaic
 * Date: 2018/10/25
 * Time: 2:35
 */

namespace service;

use PHPExcel;

class ExcelService {

    public static function exportExcel() {
        $objExcel = new PHPExcel();
        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');

        // 设置水平垂直居中
        $objExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        // 字体和样式
        $objExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
        $objExcel->getActiveSheet()->getStyle('A2:AB2')->getFont()->setBold(true);
        $objExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);

        // 第一行、第二行的默认高度
        $objExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
        $objExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);

        //设置某一列的宽度
        $objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
        $objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);

        // 设置表头
        // 合并
        $objExcel->getActiveSheet()->mergeCells('A1:E1');
        $objActSheet = $objExcel->getActiveSheet(0);

        //设置excel的标题
        $objActSheet->setTitle('销售统计');
        $objActSheet->setCellValue('A1','销售统计');

        $objActSheet->setCellValue('A2','日期');
        $objActSheet->setCellValue('B2','订单数量');
        $objActSheet->setCellValue('C2','购买人数');
        $objActSheet->setCellValue('D2','门票销量');
        $objActSheet->setCellValue('E2','销售额');

        $driver = array();
        for ($i=0; $i<10; $i++) {
            $driver[$i]['title'] = 123;
            $driver[$i]['amount'] = 123;
            $driver[$i]['user_interest_rate'] = 123;
            $driver[$i]['start_time'] = 123;
            $driver[$i]['duration_collect'] = 123;
            $driver[$i]['daysales_amount'] = 123;
            $driver[$i]['debit_name'] = 123;
        }

        $baseRow = 3; //数据从N-1行开始往下输出 这里是避免头信息被覆盖
        foreach ( $driver as $r => $d ) {
            $i = $baseRow + $r;
            $objExcel->getActiveSheet()->setCellValue('A'.$i,$d['title']);
            $objExcel->getActiveSheet()->setCellValue('B'.$i,$d['amount']);
            $objExcel->getActiveSheet()->setCellValue('C'.$i,$d['user_interest_rate']);
            $objExcel->getActiveSheet()->setCellValue('D'.$i,$d['start_time']);
            $objExcel->getActiveSheet()->setCellValue('E'.$i,''.$d['duration_collect']);
        }

        $objExcel->setActiveSheetIndex(0);
        $fileName = "statistic_" . date('Y-m-d') . ".xlsx";

        // type1: 保存文件
        $objWriter->save($fileName);

        //type2: 输出
        header('Content-Type: applicationnd.ms-excel');
        header("Content-Disposition: attachment;filename={$fileName}");
        header('Cache-Control: max-age=0');
        $objWriter->save('php://output');
    }
}